]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
tests: Fix check for global scope pollution
authorFlorian Müllner <fmuellner@gnome.org>
Sun, 21 Jan 2024 17:50:54 +0000 (18:50 +0100)
committerMarge Bot <marge-bot@gnome.org>
Fri, 26 Jan 2024 13:12:40 +0000 (13:12 +0000)
Since commit c570011 dropped the `with` statement, this bit of
the test has only checked for writes to a specific variable
in the file.

There is no direct replacement for `with` here, the best we can
do (I think) is comparing property names on the global objects
before and after the `eval()` call.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3132>

tests/unit/jsParse.js

index 9dac85c1960439e5dc0ead1e5ebf7561d3bff83e..37e2e0cb93ea11a281ea680baac8187b2054cac7 100644 (file)
@@ -210,8 +210,8 @@ for (let i = 0; i < testsIsUnsafeExpression.length; i++) {
 
 for (let i = 0; i < testsModifyScope.length; i++) {
     let text = testsModifyScope[i];
-    // We need to use var here for the with statement
-    var obj = {};
+
+    const globalPropsPre = Object.getOwnPropertyNames(globalThis).sort();
 
     // Just as in JsParse.getCompletions, we will find the offset
     // of the expression, test whether it is unsafe, and then eval it.
@@ -232,6 +232,8 @@ for (let i = 0; i < testsModifyScope.length; i++) {
             }
         }
     }
-    let propertyNames = Object.getOwnPropertyNames(obj);
-    JsUnit.assertEquals(`The context '${JSON.stringify(obj)}' was not modified`, propertyNames.length, 0);
+
+    const globalPropsPost = Object.getOwnPropertyNames(globalThis).sort();
+    Assertions.assertArrayEquals('The global object was not modified',
+        globalPropsPre, globalPropsPost);
 }